package Arrays;

public class _717_1bitand2bitCharacters {
    // circle from the end to the beginning,if meet 0 break,because 0 is the
    //partition of two signle substring,and then count the sum of 1,if is even
    //return true,else return false;
    public boolean isOneBitCharacter(int[] bits) {
        if(bits.length==1){
            return true;
        }
        if (bits.length == 2) {
            if (bits[0] == 1) {
                return false;
            } else {
                return true;
            }
        }
        int count = 0;
        for(int k=bits.length-2;k>=0;k--) {
            if (bits[k] == 0) {
                break;
            } else {
                count++;
            }
        }
        if (count % 2 == 0) {
            return true;
        } else {
            return false;
        }
    }

    //more simplify
    public boolean isOneBitCharacter1_1(int[] bits) {
        int index = -1;
        for(int i = bits.length - 2; i >= 0; i--) {
            if(bits[i] == 0) {
                index = i;
                break;
            }

        }
        return ((bits.length-index)%2 == 0);
    }

    //from beginning to the end
    public boolean isOneBitCharacter1(int[] bits) {
        int i = 0;
        while(i<bits.length-1){
            if(bits[i]==0){
                i++;
            }else {
                i +=2;
            }
        }
        return i==bits.length-1;
    }
}
